package com.dddd.blog.blogadmin.controller;


import com.dddd.blog.api.admin.feign.PostInfoClient;
import com.dddd.blog.api.admin.feign.UserInfoClient;
import com.dddd.blog.api.admin.pojo.Admin;
import com.dddd.blog.blogadmin.service.IAdminService;
import com.dddd.blog.blogcommon.constant.ResultCode;
import com.dddd.blog.blogcommon.controller.BaseController;
import com.dddd.blog.blogcommon.pojo.Result;
import com.dddd.blog.blogcommon.pojo.vo.AdminQueryVO;
import com.dddd.blog.blogcommon.pojo.vo.AdminVO;
import com.dddd.blog.blogcommon.utils.JwtUtils;
import com.dddd.blog.blogcommon.utils.ObjectInvertUtils;
import com.dddd.blog.blogcommon.utils.PasswordUtils;
import com.dddd.blog.blogcommon.utils.ResultUtils;
import io.jsonwebtoken.Claims;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 *  用于管理员(Admin)相关操作的接口
 * @author dddd
 * @since 2022-03-08
 */
@RestController
@RequestMapping("/admin")
public class AdminController extends BaseController {

    @Autowired
    private IAdminService adminService;

    @Autowired
    private UserInfoClient userInfoClient;

    @Autowired
    private PostInfoClient postInfoClient;

    /**
     *
     * @param vo 登录请求对象
     * @return
     */
    @PostMapping("/login")
    @ApiOperation(value = "请求登录并获取Token")
    public Result doLogin(@RequestBody AdminQueryVO vo){

        this.getLogger().info(vo.toString());

        String name = vo.getUserName();
        String password = vo.getPassword();

        Admin admin = adminService.getAdmin(name, password);

        getLogger().info(admin.toString());

        // 对象不存在
        if(admin == null){
            return ResultUtils.error(ResultCode.Object_NOT_EXIST);
        }

        // 验证密码
        String salt = admin.getSalt();
        boolean correct = PasswordUtils.isCorrectPassword(password, salt, admin.getPassword());
        if(!correct){
            Result error = ResultUtils.error(ResultCode.PARAMS_INVLIDA);
            error.setMessage("密码错误");
            return error;
        }

        //生成token
        long expireMills = 2*60*60*1000;
        String access_token = JwtUtils.createToken(admin.getName(), expireMills);
        String refresh_token = JwtUtils.createToken(admin.getName(), 7*24*60*60*1000);

        // 返回token
        // 同时返回一个刷新token
        Map<String,Object> map = new HashMap<>();
        map.put("access_token",access_token);
        map.put("expires_in", expireMills);
        map.put("refresh_token",refresh_token);


        return ResultUtils.success(map);
    }

    /**
     *
     * @param refresh_token
     * @return
     */
    @PostMapping("/refresh")
    @ApiOperation(value = "进行刷新token操作")
    public Result doRefresh(@ApiParam("refresh_token") String refresh_token){


        return null;
    }


    /**
     * 获取用户信息
     * @param token
     * @return
     */
    @GetMapping("/info")
    @ApiOperation(value = "根据token获取用户信息")
    public Result getUserInfo(@RequestParam String token){

        // 传递空的token，响应401
        if(StringUtils.isEmpty(token)){
            // 401响应
            return ResultUtils.error(ResultCode.UNAUTHORIED);
        }

        boolean expired = JwtUtils.isExpired(token);

        if(expired){
            // token过期
            Result result = ResultUtils.error(ResultCode.UNAUTHORIED);
            result.setMessage("token过期");
            return result;
        }

        // 解析token获取用户信息
        Claims claims = JwtUtils.parseToken(token);

        String username = claims.get("username", String.class);

        Admin rtValue = adminService.getAdminByName(username);

        AdminVO vo = ObjectInvertUtils.invert(rtValue, AdminVO.class);

        return ResultUtils.success(vo);

    }

    @RequestMapping("/statictis/info")
    @ApiOperation(value = "获取前端首页的统计信息")
    public Result getStatictisInfo(){

        Map<String,Object> info = new HashMap();

        Result userResult = userInfoClient.getUserCount();

        Result postResult = postInfoClient.getPostCount();

        info.put("postCount",postResult.getData());
        info.put("userCount",userResult.getData());

        return ResultUtils.success(info);
    }

    @GetMapping("/userList")
    @ApiOperation(value = "获取用户列表")
    public Result getUserList(){
        return null;
    }

    @PostMapping("/userList")
    @ApiOperation(value = "获取用户列表")
    public Result getUserListByPage(@RequestBody Object obbject){
        return null;
    }
}
